package org.adl.registry.types;

import net.handle.api.HSAdapter;
import net.handle.hdllib.HandleException;
import net.handle.hdllib.HandleValue;
import org.adl.registry.handles.HandleUtil;

/**
 *
 * @author Jacob Marks - Joint ADL Co-Lab
 * @version 1.0
 * @license This software is licensed as open source software pursuant to the terms defined in the Joint ADL Co-Lab Open Source Software License.  Use of any part of this software constitutes acceptance of this license. The Joint ADL Co-Lab open source agreement is available at {@link http://adlregistry.adlnet.gov/source_license.html}.
 */
public class Handle
{
    public static final String HANDLE_SEPERATOR = "/";
    public static final String EMAIL_VALUE_TYPE = "EMAIL";
    public static final String HS_ADMIN_TYPE = "HS_ADMIN";
    public static final String HS_SECKEY_TYPE = "HS_SECKEY";
    public static final String HS_SERV_TYPE = "HS_SERV";
    public static final String HS_SITE_TYPE = "HS_SITE";
    public static final String GROUP_TYPE = "100.TYPES/GROUP";
    public static final String USER_TYPE = "100.TYPES/ID";
    
    private String mHandleName;
    private HandleValue[] mHandleValues;
    private boolean mHandleExists;
    private HSAdapter mHandleApi;
    
    /** Creates a new instance of Handle */
    public Handle(String iHandle, HSAdapter iApi)
    throws HandleException
    {
        this.mHandleName = iHandle;
        this.mHandleApi = iApi;
        this.mHandleValues = new HandleValue[0];
        this.mHandleExists = false;
        
        try
        {
            this.mHandleValues = iApi.resolveHandle(iHandle, null, null);
            
            this.mHandleExists = true;
        }
        catch (HandleException ex)
        {
            if (ex.getCode() == ex.HANDLE_DOES_NOT_EXIST)
            {
                this.mHandleExists = false;
            }
            else
            {
                throw ex;
            }
        }
    }
    
    public boolean getHandleExists()
    {
        return this.mHandleExists;
    }
    
    public HandleValue[] getHandleValues()
    {
        return this.mHandleValues;
    }
    
    protected void addHandleValue(String iType, String iValue)
    throws HandleException
    {
        HandleValue newValue = new HandleValue();
        
        newValue.setData(iValue.getBytes());
        newValue.setType(iType.getBytes());
        
        if(iType.equalsIgnoreCase(Handle.HS_SECKEY_TYPE))
        {
            newValue.setAnyoneCanRead(false);
            newValue.setAnyoneCanWrite(false);
        }
        
        HandleValue[] values = new HandleValue[] { newValue };
        
        HandleUtil.addHandleValue(this.mHandleName, values, this.mHandleApi);
    }
    
//    protected void create()
//    {
//        //TODO:
//        //HandleUtil.createHandle(this.mHandleName, null, this.mHandleApi);
//    }
//
//    protected HandleValue getHandleValue()
//    {
//
//    }
//
//
//    protected void updateHandleValue()
//    {
//
//    }
    
    public static final String FormatHandle(String prefix, String suffix)
    {
        String handle = "";
        
        handle = prefix + Handle.HANDLE_SEPERATOR + suffix;
        
        return handle;
    }
}
